package com.hdmall.business.common.util;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

/**
 * 参数只有list和map 如果有复杂数据类型需要把值转换成字符串
 */
public class HttpClientUtil {
    public static String doGetWithParam(String url, List<Map<String,String>> list){
        CloseableHttpClient httpClient =HttpClients.createDefault();
        //创建一个uri对象
        String rest =null;
        URIBuilder uriBuilder = null;
        HttpGet get =null;
        try {
            uriBuilder = new URIBuilder(url);
            if(null==list||list.size()==0){
                get= new HttpGet(uriBuilder.build());
            }else{
                for(Map<String,String> map:list) {
                    for (String key:map.keySet()) {
                        uriBuilder.addParameter(key, map.get(key));
                    }
                }
                get = new HttpGet(uriBuilder.build());
            }
            CloseableHttpResponse response =httpClient.execute(get);

            //取响应的结果

            int statusCode =response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            HttpEntity entity =response.getEntity();

            rest= EntityUtils.toString(entity,"utf-8");

            System.out.println(rest);

            //关闭httpclient

            response.close();

            httpClient.close();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        //执行请求
        return rest;


    }
    public static String doGetWithParam(String url, Map<String,String> map){
        CloseableHttpClient httpClient =HttpClients.createDefault();
        //创建一个uri对象
        String rest =null;
        URIBuilder uriBuilder = null;
        HttpGet get =null;
        try {
            uriBuilder = new URIBuilder(url);
            if(null==map||map.size()==0){
                get= new HttpGet(uriBuilder.build());
            }else{
                for (String key:map.keySet()) {
                    uriBuilder.addParameter(key, map.get(key));
                    }
                get = new HttpGet(uriBuilder.build());
            }
            CloseableHttpResponse response =httpClient.execute(get);

            //取响应的结果

            int statusCode =response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            HttpEntity entity =response.getEntity();

            rest= EntityUtils.toString(entity,"utf-8");

//            System.out.println(rest);

            //关闭httpclient

            response.close();

            httpClient.close();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        //执行请求
        return rest;


    }

    public static String doPostWithParam(String url, Map<String,String> map){
        CloseableHttpClient httpClient =HttpClients.createDefault();
        //创建一个uri对象
        String rest =null;
        URIBuilder uriBuilder = null;
        HttpPost post =null;
        try {
            uriBuilder = new URIBuilder(url);
            if(null==map||map.size()==0){
                post= new HttpPost(uriBuilder.build());
            }else{
                for (String key:map.keySet()) {
                    uriBuilder.addParameter(key, map.get(key));
                }
                post = new HttpPost(uriBuilder.build());
            }
            CloseableHttpResponse response =httpClient.execute(post);

            //取响应的结果

            int statusCode =response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            HttpEntity entity =response.getEntity();

            rest= EntityUtils.toString(entity,"utf-8");


            //关闭httpclient

            response.close();

            httpClient.close();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        //执行请求
        return rest;


    }

    public static String doPostWithParam(String url){
        CloseableHttpClient httpClient =HttpClients.createDefault();
        //创建一个uri对象
        String rest =null;
        URIBuilder uriBuilder = null;
        HttpPost post =null;
        try {
            uriBuilder = new URIBuilder(url);
            post= new HttpPost(uriBuilder.build());
            CloseableHttpResponse response =httpClient.execute(post);

            //取响应的结果

            int statusCode =response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            HttpEntity entity =response.getEntity();

            rest= EntityUtils.toString(entity,"utf-8");

            //关闭httpclient

            response.close();

            httpClient.close();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        //执行请求
        return rest;


    }

    public static String doGetWithParam(String url){
        CloseableHttpClient httpClient =HttpClients.createDefault();
        //创建一个uri对象
        String rest =null;
        URIBuilder uriBuilder = null;
        HttpGet get =null;
        try {
            uriBuilder = new URIBuilder(url);
            get= new HttpGet(uriBuilder.build());
            CloseableHttpResponse response =httpClient.execute(get);

            //取响应的结果

            int statusCode =response.getStatusLine().getStatusCode();

            System.out.println(statusCode);

            HttpEntity entity =response.getEntity();

            rest= EntityUtils.toString(entity,"utf-8");


            //关闭httpclient

            response.close();

            httpClient.close();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        catch (IOException e) {
            e.printStackTrace();
            System.out.println("不能访问URL路径");
        }
        //执行请求
        return rest;


    }

    public static void main(String[] args) {
        //Map<String,String> map=new HashMap<String,String>();
        //map.put("aaa","aaaa");
        //map.put("bbb","bbbb");
       /* String r=doGetWithParam("http://localhost:9090/task/taskOrderContEndOrderTime");
        System.out.println(r);*/
    }
}
